define(function (require, exports, module) {
    var angular = require('angular');
    require("angular-ui");
    require("angular-route");

    var beeMemberModule = angular.module('beeMember', [])
        .constant('api.member.path', "/api/index.php?r=xmf/xmf-user/")
        .factory('MemberService', [
            '$http',
            'api.member.path',
            function ($http, api_path) {
                var httpClient = {
                    get: function (api, data) {
                        var url = api_path + api;
                        var search = (data && data.search) || {};
                        angular.forEach(search, function (val, key) {
                            url += "&" + key + "=" + val;
                        });

                        return $http.get(url)
                            .then(function (res) {
                                res = res.data;
                                if (res.retCode == "0") {
                                    return res;
                                }
                            })
                    },
                    post: function (api, data) {
                        return $http.post(api_path + api, (data && data.data) || {})
                            .then(function (res) {
                                res = res.data;
                                if (res.retCode == "0") {
                                    return res;
                                }
                            })
                    }
                }
                return {
                    getMemberList: function (data) {
                        return httpClient.post('list', data)
                    },
                    searchMembers: function (data) {
                        return httpClient.post('search', data)
                    },
                    disableMember: function (data) {
                        return httpClient.post('disable', data)
                    },
                    enableMember: function (data) {
                        return httpClient.post('enable', data)
                    },
                    updateMember: function (data) {
                        return httpClient.post('edit', data)
                    },
                    deleteMember: function (data) {
                        return httpClient.post('delete', data)
                    },
                    resetMemberPassword: function (data) {
                        return httpClient.post('reset-pwd', data)
                    },
                    createMember: function (data) {
                        return httpClient.post('add', data)
                    }

                }
            }
        ])
        .controller('MemberController', [
            '$scope',
            '$rootScope',
            'MemberService',
            function ($scope, $rootScope, service) {

                $scope.createMember = function (scope, $data, $dialog) {
                    if (!$data || !$data.mobile_tel || !$data.user_name) {
                        $rootScope.$emit('error', '数据填写不完整')
                        return false;
                    }
                    service.createMember({
                        data: {
                            mobile: $data.mobile_tel,
                            user_name: $data.user_name
                        }
                    }).then(function (res) {
                        $scope.$emit('refresh', { pageIndex: 1 });
                        $dialog.close();
                    });

                    return false;
                };
                $scope.disableMember = function (member) {
                    service.disableMember({
                        data: {
                            user_id: member.user_id
                        }
                    }).then(function (res) {
                        $scope.$emit('refresh');
                    })
                };
                $scope.enableMember = function (member) {
                    service.enableMember({
                        data: {
                            user_id: member.user_id
                        }
                    }).then(function (res) {
                        $scope.$emit('refresh');
                    })
                };
                $scope.enableMembers = function () {
                    var selects = [];

                    for (var i = 0; i < $scope.pager.pageSize; i++) {
                        if ($scope.memberList[i] && $scope.memberList[i].checked) {
                            selects.push($scope.memberList[i].user_id)
                        }
                    }

                    if (!selects.length) {
                        return;
                    }
                    service.enableMember({
                        data: {
                            user_id: selects.join(',')
                        }
                    }).then(function (res) {
                        $scope.getMemberList();
                    })
                };

                $scope.disableMembers = function () {
                    var selects = [];

                    for (var i = 0; i < $scope.pager.pageSize; i++) {
                        if ($scope.memberList[i] && $scope.memberList[i].checked) {
                            selects.push($scope.memberList[i].user_id)
                        }
                    }

                    if (!selects.length) {
                        return;
                    }
                    service.disableMember({
                        data: {
                            user_id: selects.join(',')
                        }
                    }).then(function (res) {
                        $scope.$emit('refresh');
                    })
                };
                $scope.updateMember = function (scope, $data, $dialog) {
                    service.updateMember({
                        data: {
                            mobile: $data.mobile_tel,
                            user_name: $data.user_name,
                            user_id: $data.user_id
                        }
                    }).then(function (res) {
                        $scope.$emit('refresh');
                    })
                };
                $scope.deleteMember = function (member) {
                    service.deleteMember({
                        data: {
                            user_id: member.user_id
                        }
                    }).then(function (res) {
                        $scope.$emit('refresh', { pageIndex: 1 });
                    })
                };
                $scope.resetMemberPassword = function (scope, $data, $dialog) {
                    service.resetMemberPassword({
                        data: {
                            user_id: $data.user_id,
                            user_pwd: $data.user_pwd
                        }
                    }).then(function (res) {
                        $scope.$emit('refresh');
                    })
                };

                $scope.getMemberList = function (data) {
                    data = data || {};
                    var pageIndex = data.pageIndex || $scope.pager.pageIndex || 1;
                    service.getMemberList({
                        data: {
                            "page_index": pageIndex,
                            "page_size": data.pageSize || 10,
                            "isdisable": data.status || "2"
                        }
                    }).then(function (res) {
                        $scope.memberList = res.data.data;
                        $scope.pager.total = res.data.total;
                        $scope.checker.all = false;
                        $scope.checker.selected = false;
                        $scope.pager.pageIndex = pageIndex;
                    })
                },

                $scope.search = function (data) {
                    data = data || {};
                    $scope.keyword = $scope.search_text;
                    service.searchMembers({
                        data: {
                            "page_index": data.pageIndex || 1,
                            "page_size": data.pageSize || 10,
                            "isdisable": data.status || "2",
                            "keyword": $scope.keyword || ""
                        }
                    }).then(function (res) {
                        $scope.memberList = res.data.data;
                        $scope.pager.total = res.data.total;
                        $scope.pager.pageIndex = data.pageIndex || 1;
                    })
                };

                $scope.pager = {
                    pageIndex: 0,
                    pageSize: 10,
                    changed: function (pageIndex) {
                        if ($scope.keyword) {
                            $scope.search({
                                pageIndex: pageIndex,
                                pageSize: $scope.pager.pageSize
                            })
                        } else {
                            $scope.getMemberList({
                                pageIndex: pageIndex,
                                pageSize: $scope.pager.pageSize
                            })
                        }
                    }
                };

                $scope.checker = {
                    all: false,
                    checkAll: function () {
                        for (var i = 0; i < $scope.pager.pageSize; i++) {
                            if ($scope.memberList[i]) {
                                $scope.memberList[i].checked = $scope.checker.all;
                            }
                        }

                        $scope.checker.selected = $scope.checker.all;
                    },
                    check: function (user) {
                        var all = true;
                        $scope.checker.selected = false;

                        for (var i = 0; i < $scope.pager.pageSize; i++) {
                            if ($scope.memberList[i]) {
                                if ($scope.memberList[i].checked) {
                                    $scope.checker.selected = true;
                                } else {
                                    all = false;
                                }
                            }
                        }

                        $scope.checker.all = all;
                    }
                };

                $scope.$on('refresh', function (e, data) {
                    if (data) {
                        $scope.pager.pageIndex = 1;
                    }
                    $scope.getMemberList(data);
                });

                $scope.$emit('refresh');


            }
        ])

});
